package org.aplikator.client.panels;

import com.google.gwt.dom.client.Style;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ScrollEvent;
import com.google.gwt.user.client.Window.ScrollHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

public class GlassPanel extends Composite implements ResizeHandler, ScrollHandler {

    private SimplePanel panel = new SimplePanel();

    public GlassPanel() {
        initWidget(panel);
        Style style = getElement().getStyle();
        style.setProperty("backgroundColor", "black");
        style.setProperty("filter", "alpha(opacity=50)");
        style.setProperty("opacity", "0.5");
        style.setProperty("left", "0");
        style.setProperty("top", "0");
        style.setProperty("position", "absolute");
        Window.addResizeHandler(this);
        Window.addWindowScrollHandler(this);

    }

    public void onResize(ResizeEvent event) {
        resize();
    }

    public void show() {
        Element elem = panel.getElement();
        DOM.setStyleAttribute(elem, "left", "" + Window.getScrollLeft());
        DOM.setStyleAttribute(elem, "top", "" + Window.getScrollTop());
        DOM.setStyleAttribute(elem, "position", "absolute");
        resize();
        RootPanel.get().add(this);
    }

    public void hide() {
        RootPanel.get().remove(this);
    }

    private void resize() {

        panel.setSize(Window.getClientWidth() + "px", Window.getClientHeight() + "px");
    }

    public void onWindowScroll(ScrollEvent event) {
        Element elem = panel.getElement();
        DOM.setStyleAttribute(elem, "left", "" + event.getScrollLeft());
        DOM.setStyleAttribute(elem, "top", "" + event.getScrollTop());

    }
}